package com.swe.chums.dao.impl;

import com.swe.chums.dao.ActivityTypeDao;
import com.swe.chums.dao.model.ActivityType;
import com.swe.chums.dao.model.ActivityTypePk;
import com.swe.chums.dao.exceptions.ActivityTypeDaoException;
import java.util.List;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.transaction.annotation.Transactional;

public class ActivityTypeDaoImpl extends AbstractDAO implements ParameterizedRowMapper<ActivityType>, ActivityTypeDao
{
	protected SimpleJdbcTemplate jdbcTemplate;

	protected DataSource dataSource;

	/**
	 * Method 'setDataSource'
	 * 
	 * @param dataSource
	 */
	public void setDataSource(DataSource dataSource)
	{
		this.dataSource = dataSource;
		jdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	/**
	 * Method 'insert'
	 * 
	 * @param dto
	 * @return ActivityTypePk
	 */
	@Transactional
	public ActivityTypePk insert(ActivityType dto)
	{
		jdbcTemplate.update("INSERT INTO " + getTableName() + " ( name ) VALUES ( ? )",dto.getName());
		ActivityTypePk pk = new ActivityTypePk();
		pk.setId( jdbcTemplate.queryForInt("select last_insert_id()") );
		return pk;
	}

	/** 
	 * Updates a single row in the activity_type table.
	 */
	@Transactional
	public void update(ActivityTypePk pk, ActivityType dto) throws ActivityTypeDaoException
	{
		jdbcTemplate.update("UPDATE " + getTableName() + " SET id = ?, name = ? WHERE id = ?",dto.getId(),dto.getName(),pk.getId());
	}

	/** 
	 * Deletes a single row in the activity_type table.
	 */
	@Transactional
	public void delete(ActivityTypePk pk) throws ActivityTypeDaoException
	{
		jdbcTemplate.update("DELETE FROM " + getTableName() + " WHERE id = ?",pk.getId());
	}

	/**
	 * Method 'mapRow'
	 * 
	 * @param rs
	 * @param row
	 * @throws SQLException
	 * @return ActivityType
	 */
	public ActivityType mapRow(ResultSet rs, int row) throws SQLException
	{
		ActivityType dto = new ActivityType();
		dto.setId( rs.getInt( 1 ) );
		dto.setName( rs.getString( 2 ) );
		return dto;
	}

	/**
	 * Method 'getTableName'
	 * 
	 * @return String
	 */
	public String getTableName()
	{
		return "spring2013g3.activity_type";
	}

	/** 
	 * Returns all rows from the activity_type table that match the criteria 'id = :id'.
	 */
	@Transactional
	public ActivityType findByPrimaryKey(int id) throws ActivityTypeDaoException
	{
		try {
			List<ActivityType> list = jdbcTemplate.query("SELECT id, name FROM " + getTableName() + " WHERE id = ?", this,id);
			return list.size() == 0 ? null : list.get(0);
		}
		catch (Exception e) {
			throw new ActivityTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the activity_type table that match the criteria ''.
	 */
	@Transactional
	public List<ActivityType> findAll() throws ActivityTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, name FROM " + getTableName() + " ORDER BY id", this);
		}
		catch (Exception e) {
			throw new ActivityTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the activity_type table that match the criteria 'id = :id'.
	 */
	@Transactional
	public List<ActivityType> findWhereIdEquals(int id) throws ActivityTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, name FROM " + getTableName() + " WHERE id = ? ORDER BY id", this,id);
		}
		catch (Exception e) {
			throw new ActivityTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns all rows from the activity_type table that match the criteria 'name = :name'.
	 */
	@Transactional
	public List<ActivityType> findWhereNameEquals(String name) throws ActivityTypeDaoException
	{
		try {
			return jdbcTemplate.query("SELECT id, name FROM " + getTableName() + " WHERE name = ? ORDER BY name", this,name);
		}
		catch (Exception e) {
			throw new ActivityTypeDaoException("Query failed", e);
		}
		
	}

	/** 
	 * Returns the rows from the activity_type table that matches the specified primary-key value.
	 */
	public ActivityType findByPrimaryKey(ActivityTypePk pk) throws ActivityTypeDaoException
	{
		return findByPrimaryKey( pk.getId() );
	}

}
